<?php

class Controller_ProfessionArticle extends Controller_Website {

    public function action_list() {
        $where = array();
        $where['status'] = Arr::get($_GET, 'status');
        $where['cid'] = Arr::get($_GET, 'cid');
        $where['ORDER'] = 'id DESC';
        $where = array_filter($where, 'strlen');
        
        $title = Arr::get($_GET, 'title');
        if (!empty($title)) {
            $where['title'] = array('like'=>"%$title%");
        }
        
        $m_article = Model::factory('professionArticle');
        $total = $m_article->count($where);
        $pager = new Pager($total, 10);
        $list = $m_article->select($pager->offset, $pager->size, $where)->as_array();
        
        $m_category = Model::factory('ProfessionCategory');
        $articleCategory = $m_category->getAll();
        
        $this->content = View::factory('professionArticle/list');
        $this->content->list = $list;
        $this->content->pager = $pager;
        $this->content->articleCategory = $articleCategory;

    }

    public function action_add() {
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $data['add_time'] = time();
            $m_article = Model::factory('professionArticle');
            $ret = $m_article->insert($data);
            if ($ret !== false) {
                $this->redirect('professionArticle/list');
            }
        }
        $m_category = Model::factory('ProfessionCategory');
        $cat_list = $m_category->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $this->content = View::factory('professionArticle/add');
        $this->content->info = null;
        
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
    }

    public function action_edit() {
        $id = Arr::get($_GET, 'id');
        $m_article = Model::factory('professionArticle');
        $info = $m_article->getRowById($id);
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $ret = $m_article->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('professionArticle/list');
            }
        }
        
        $m_category = Model::factory('ProfessionCategory');
        $cat_list = $m_category->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
//         $info['pics'] = array();
        
//         $pics = explode('|', $info['pic']);
//         foreach ($pics as $pic) {
//             $info['pics'][] = $this->_format_pic($pic);
//         }
        
        $this->content = View::factory('professionArticle/edit');
        $this->content->info = $info;
        
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
    }

    public function action_del() {
        $id = $_GET['id'];
        $m_article = Model::factory('professionArticle');
        $ret = $m_article->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    public function action_close() {
        $id = $_GET['id'];
        $data = array(
            'status' => '0',
        );
        $m_article = Model::factory('professionArticle');
        $ret = $m_article->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open() {
        $id = $_GET['id'];
        $data = array(
            'status' => '1',
        );
        $m_article = Model::factory('professionArticle');
        $ret = $m_article->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    protected function _get_data($post) {
        $data = array_intersect_key($post, array_flip(array('cid','title','desc','pic','content')));
        if (!empty($data['pic'])) {
            $data['pic'] = array_pop($data['pic']);
        }
        $data['edit_time'] = strtotime('now');
        return $data;
    }
    
    protected function _format_pic($pic) {
        if (empty($pic)) {
            return '';
        }
        if(strpos($pic, '?')) {
            $pic = strstr($pic, '?', true);
        }
        if (strpos($pic, '://') !== false) {
            $pic = $pic.'?x-oss-process=image/resize,m_fill,h_200,w_200';
        } else {
            $pic = 'http://jtkj.oss-cn-beijing.aliyuncs.com/' . $pic.'?x-oss-process=image/resize,m_fill,h_200,w_200';
        }
    
        return $pic;
    }
}
